/**
 * @project heima-leadnews
 * @author MECHREVO
 * @date 2024/1/12 11:35:43
 * @version 1.0
 */
package com.heima.article.service.Impl;

import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.heima.article.Mapper.ApArticleConfigMapper;
import com.heima.article.Mapper.ApArticleContentMapper;
import com.heima.article.Mapper.ApArticleMapper;
import com.heima.article.service.ApArticleService;
import com.heima.article.service.FreemarkerService;
import com.heima.model.article.dto.ArticleDto;
import com.heima.model.article.dto.ArticleHomeDto;
import com.heima.model.article.pojo.ApArticle;
import com.heima.model.article.pojo.ApArticleConfig;
import com.heima.model.article.pojo.ApArticleContent;
import com.heima.model.common.dtos.ResponseResult;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;
import java.util.Map;

@Service
@Transactional
@Slf4j
public class ApArticleServiceImpl extends ServiceImpl<ApArticleMapper, ApArticle> implements ApArticleService {
    @Autowired
    private ApArticleMapper apArticleMapper;
    @Autowired
    private ApArticleConfigMapper apArticleConfigMapper;
    @Autowired
    private ApArticleContentMapper apArticleContentMapper;
    @Autowired
    private FreemarkerService freemarkerService;
    @Override
    public ResponseResult load(ArticleHomeDto articleHomeDto, int type) {
        List<ApArticle> list=apArticleMapper.load(articleHomeDto,type);
        return ResponseResult.okResult(list);
    }

    @Override
    public ResponseResult saveArticle(ArticleDto dto) {
        ApArticle article=new ApArticle();
        BeanUtils.copyProperties(dto,article);
        if(dto.getId()==null){
            apArticleMapper.insert(article);
            ApArticleConfig apArticleConfig=new ApArticleConfig(article.getId());
            apArticleConfigMapper.insert(apArticleConfig);
            ApArticleContent content = new ApArticleContent();
            content.setContent(dto.getContent());
            content.setArticleId(article.getId());
            apArticleContentMapper.insert(content);
            dto.setId(article.getId());
        }else{
            apArticleMapper.updateById(article);
            ApArticleContent content = apArticleContentMapper.selectOne(Wrappers.lambdaQuery(ApArticleContent.class)
                    .eq(ApArticleContent::getArticleId, dto.getId()));
            content.setContent(dto.getContent());
            apArticleContentMapper.updateById(content);
        }
        freemarkerService.builpage(dto);
        return ResponseResult.okResult(String.valueOf(article.getId()));
    }

    @Override
    public void downOrUpArticle(Map<String, Object> map) {
        //先拿到文章的id和enable的值
        String articleId = map.get("articleId").toString();
        Short enable = Short.valueOf(map.get("enable").toString());

        boolean isDown=false;
        if(enable==0){
            isDown=true;
        }

        ApArticleConfig apArticleConfig = new ApArticleConfig();
        apArticleConfig.setArticleId(Long.valueOf(articleId));
        apArticleConfig.setIsDown(isDown);
        apArticleConfigMapper.update(apArticleConfig,Wrappers.lambdaUpdate(ApArticleConfig.class)
                .eq(ApArticleConfig::getArticleId,articleId)
                .set(ApArticleConfig::getIsDown,isDown));
    }
}
